package com.start.pay.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.pay.core.StartPayServerService;
import com.start.pay.core.entity.StartPayOrderEntity;
import com.start.pay.core.service.StartPayOrderService;
import com.start.security.annotations.DataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/start/platform/order")
@RegisterMenu(value = "平台订单", parentId = "fa082311783863635318d8e0ed424554", systemCode = "start:platform:order", component = "views/platform/order/index", path = "/platform/order/index")
public class StartPayOrderController {
    private static final String LOG_TITLE = "平台订单";

    @Autowired
    private StartPayOrderService startPayOrderService;


    /**
     * 列表
     */
    @GetMapping("/page")
    @DataPermission("views/platform/order/index")
    public Result<?> list(Page<StartPayOrderEntity> page, StartPayOrderEntity entity) {
        QueryWrapper<StartPayOrderEntity> queryWrapper = DataQueryConstructor.createEntity(entity);
        return Result.ok(startPayOrderService.page(page, queryWrapper));
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('start:platform:order:info')")
    @RegisterPermission(value = "start:platform:order:info", name = "查看")
    public Result<?> info(@PathVariable("id") String id) {
        return Result.ok(startPayOrderService.getById(id));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('start:platform:order:add')")
    @RegisterPermission(value = "start:platform:order:add", name = "添加")
    public Result<?> save(@RequestBody StartPayOrderEntity entity) {
        startPayOrderService.save(entity);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('start:platform:order:edit')")
    @RegisterPermission(value = "start:platform:order:edit", name = "修改")
    public Result<?> update(@RequestBody StartPayOrderEntity entity) {
        startPayOrderService.updateById(entity);
        return Result.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('start:platform:order:delete')")
    @RegisterPermission(value = "start:platform:order:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
        final StartPayOrderEntity entity = startPayOrderService.getById(id);
        if (entity == null) return Result.ok();
        startPayOrderService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/refundByOrderNo/{orderNo}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('start:platform:order:refund')")
    @RegisterPermission(value = "start:platform:order:refund", name = "退款")
    public Result<?> refundByOrderNo(@PathVariable("orderNo") String orderNo) {
        StartPayServerService.getInstance().refundOrder(orderNo);
        return Result.ok();
    }


}
